package com.iflytek.esb.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iflytek.esb.service.ConfigService;
import com.iflytek.esb.util.AuthenticationFilter;
import com.iflytek.esb.vo.Menu;

@Controller
public class HomeController {

	private static final Logger LOG = Logger.getLogger(HomeController.class);
	
	private static final Menu[] MENUS = new Menu[] {
		new Menu("", "服务", "", new Menu[] {
				new Menu("", "状态", "service/infomation"),
				new Menu("", "所有服务", "service/list"),
				new Menu("", "添加服务", "service/add"),
				new Menu("", "定时器", "service/timer"),
				new Menu("", "导出", "service/export"),
				new Menu("", "导入", "service/import")
		}),
		new Menu("", "监控", "PARTY", new Menu[] {
				new Menu("", "性能", "monitor/perfermence"),
				new Menu("", "响应", "monitor/feedback"),
				new Menu("", "处理器", "monitor/processor"),
				new Menu("", "消息跟踪", "monitor/trace"),
				new Menu("", "异常", "monitor/failure")
		}),
		new Menu("", "设置", "", new Menu[] {
				new Menu("", "数据库", "system/database"),
				new Menu("", "功能", "system/service"),
				new Menu("", "日志", "system/log")
		}),
		new Menu("", "工具", "RESOURCE", new Menu[] {
				new Menu("", "Java调用", "tools/wsdl2java"),
				new Menu("", ".NET调用", "tools/wsdl2net"),
				new Menu("", "WSDL 验证", "tools/wsdlvalidation"),
		}),
	};
	
	@Autowired
	private ConfigService configService;
	
	@RequestMapping("/admin/index")
	public ModelAndView index() {
		LOG.info("<open index page>");
		
		String acct = AuthenticationFilter.getLoginAccount();
		LOG.debug("login account: " + acct);
		
		ModelAndView mv = new ModelAndView("index");
		
		Menu[] subMenus = getSubMenus(0);
		mv.addObject("topMenus", MENUS);
		mv.addObject("subMenus", subMenus);
		
		mv.addObject("applicationTitle", configService.getConfigValue("application.title"));
		mv.addObject("loginAccount", acct);
		
		return mv;
	}

	@RequestMapping("/login")
	public ModelAndView login(HttpSession session, String accountName, String password) {
		if (StringUtils.isBlank(accountName) && StringUtils.isBlank(password)) {
			ModelAndView mv = new ModelAndView("login");
			mv.addObject("applicationTitle", configService.getConfigValue("application.title"));
			return mv;
		}
		else {
			return doLogin(session, accountName, password);
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		@SuppressWarnings("unchecked")
		Enumeration<String> names = session.getAttributeNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			session.removeAttribute(name);
		}
		return "redirect:/login";
	}

	@RequestMapping("/admin/index/menus")
	@ResponseBody
	public Menu[] getIndexPageMenus(String id) {
		int index = 0;
		if (StringUtils.contains(id, "_")) {
			String s = StringUtils.substring(id, StringUtils.indexOf(id, "_") + 1);
			index = Integer.parseInt(s);
		}
		else {
			index = Integer.parseInt(id);
		}
		
		Menu[] subs = getSubMenus(index);
		return subs;
	}

	private ModelAndView doLogin(HttpSession session, String accountName, String password) {

		//TODO 测试用登录名
		if (StringUtils.equals(accountName, "admin")) {
			session.setAttribute(AuthenticationFilter.LOGIN_ACCOUNT_SESSION_KEY, accountName);
			
			LOG.info("user login: " + accountName);
			ModelAndView mv = new ModelAndView("redirect:admin/index");
			return mv;
		}
		else {
			
			return loginError("登录账号或密码错误");
		}
	}

	private ModelAndView loginError(String message) {
		ModelAndView mv = new ModelAndView("login");
		mv.addObject("applicationTitle", configService.getConfigValue("application.title"));
		mv.addObject("message", message);
		return mv;
	}

	private Menu[] getSubMenus(int index) {
		if (index < 0 || index >= MENUS.length) {
			return new Menu[0];
		}
		
		Menu menu = MENUS[index];
		return menu.getChildren();
	}
}
